clear all 
set more off
*set maxvar 20000

cd "/Users/yasenov/Dropbox/June 2016/Writeup/Economic Inquiry/EI R and R"
use "/Users/yasenov/Dropbox/data_dta/master.dta"
drop if substr(subject, 1, 2) == "it" | substr(subject, 1, 2) == "cs" 

tostring id grade year, replace
gen course = grade + " " + subject + " " + cohort
gen class = year + " " + course
gen studentxclass = id + " " + class
gen gradebook = year + grade + cohort
destring id grade year, replace

collapse (mean) marks, by(male month class)					

* need to merge the following  vars: late_start teacher_male
* 1st. create a dictionary for these variables: 
* use master.dta; bysort class month: keep if _n == 1; keep class month late_start teacher_male; save
merge m:1 month class using vas_dict

gen malelate = male*late_start
gen maleteachermale = male*teacher_male

***************************************
***************************************
eststo clear

eststo: xi: qui reg marks i.male, vce(cluster class)
estadd local classfe " "
estadd local monthfe " "
estadd local controls " "  
eststo: xi: qui reg marks i.male malelate maleteachermale teacher_male late_start, vce(cluster class)
estadd local classfe " "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui reg marks i.male malelate maleteachermale teacher_male late_start i.month, vce(cluster class)
estadd local classfe " "
estadd local monthfe " X "
estadd local controls " "
eststo: xi: qui areg marks i.male malelate maleteachermale teacher_male late_start, absorb(class) vce(cluster class)
estadd local classfe " X "
estadd local monthfe " "
estadd local controls " "
eststo: xi: qui areg marks i.male malelate maleteachermale teacher_male late_start i.month, absorb(class) vce(cluster class)
estadd local classfe " X "
estadd local monthfe " X "
estadd local controls " "

label var _Imale_1 "\ \ \ Male"
label var maleteachermale "\ \ \ Male X \\ \hspace{0.3cm} Male Teacher"
label var malelate "\ \ \ Male X \\ \hspace{0.3cm} Late Start"

esttab using "results-rank.tex", replace ///
	keep(maleteachermale malelate _Imale_1) se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps ///
	scalars("classfe Class FE" "monthfe Month FE" "controls Controls") ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)") label prehead("") posthead("") 

esttab using "results-rank.tex", ///
    keep(_Imale_1 maleteachermale malelate) replace se(3) b(3) r2 star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	noconstant nogaps scalars("classfe Class FE" "monthfe Month FE" "controls Controls") ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)") label	
